package com.api.demo.test.sync;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.api.demo.dto.sync.*;
import com.api.demo.util.ApiSignUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: ym
 * @Description: 新版-同步数据
 * @Date: 2024/2/26 10:22
 * @Version: 1.0
 */
public class SyncData {
    //测试环境
    public static String requestUrl = "https://v3-api.yunyonggongl.com/sync/";
    //本地测试环境
    //    public static String requestUrl = "http://127.0.0.1:10088/sync/";
    public static Long platformId = 168L;
    public static Long agentId = 1762324663293186050L;

    public static Long taxId = 1760953193879048194L;
    public static String key = "ABCDEFG";
    public static String taxCode = "91420683MAC9HJQM7T";
    public static String companyId = "sync_000000001";
    public static String taskId = "sync_000000001";
    public static String orderId = "sync_000000003";
    public static String commonImageUrl = "https://new-yyg.oss-cn-hangzhou.aliyuncs.com/upload/1760908506445533186/20240227/d127d99c5e794fbcb163f379fb211c8b.jpeg";


    public static void main(String[] args) {
//        System.out.println(syncId());

        //商户信息同步
//        post(requestUrl + "company", companyInfoSync());


        //创客信息同步
//        post(requestUrl + "worker", workerInfoSync());

        //循环生成多几个  正常不需要使用
//        for (int i = 1; i < 10; i++){
//        post(requestUrl + "worker", workerInfoSync(i));
//        }


        //任务信息同步
//        post(requestUrl + "task", taskSync());

        //任务交付物信息同步
//        post(requestUrl + "achievement", achievementSync());

        //订单信息同步
//        post(requestUrl + "order", orderSync());

        //上传回单
//        post(requestUrl + "receipt", receiptSync());

        //发票同步接口
        post(requestUrl + "invoice", invoiceSync());

        //根据服务商ID查询任务类型
//        getTaskType(taxId);


    }


    /**
     * 商户信息同步
     */
    public static SyncDataCompanyDTO companyInfoSync() {
        SyncDataCompanyDTO syncDataCompanyDTO = new SyncDataCompanyDTO();
//        common(syncDataCompanyDTO);

        syncDataCompanyDTO.setTaxCreditCode(taxCode);
        syncDataCompanyDTO.setCompanyId(companyId);
        syncDataCompanyDTO.setCompanyName("测试商户_" + System.currentTimeMillis());
        syncDataCompanyDTO.setCompanyProvince("北京北京市丰台区花乡街道");
        syncDataCompanyDTO.setCreditCode(System.currentTimeMillis() + "");
        syncDataCompanyDTO.setLinkEmail("3906448@qq.com");
        syncDataCompanyDTO.setBusinessScope("经营范围");
        syncDataCompanyDTO.setIndustry("所属行业");
        syncDataCompanyDTO.setRegisteredCapital(new BigDecimal("200"));
        syncDataCompanyDTO.setBusinessLicense(commonImageUrl);
        syncDataCompanyDTO.setOpenLicense(commonImageUrl);
        syncDataCompanyDTO.setBankCode("1234567890");
        syncDataCompanyDTO.setBankName("招商银行");
        syncDataCompanyDTO.setOfficePicture(commonImageUrl);
        syncDataCompanyDTO.setCompanyMan("张三1");
        syncDataCompanyDTO.setIdCardCode("430422199604270001");
        syncDataCompanyDTO.setMobile("15099990637");
        syncDataCompanyDTO.setIdCardFront(commonImageUrl);
        syncDataCompanyDTO.setLinkMobile("15099990637");
        syncDataCompanyDTO.setLinkMan("张三");
        syncDataCompanyDTO.setContract(commonImageUrl);
        syncDataCompanyDTO.setCreateDate("2024-02-26 10:22:00");
        commonSign(syncDataCompanyDTO);
        return syncDataCompanyDTO;
    }


    /**
     * 创客信息同步
     */
    public static SyncDataWorkerDTO workerInfoSync(int i) {
        SyncDataWorkerDTO data = new SyncDataWorkerDTO();
        data.setName("张三" + i);
        data.setPhone("1234567890" + i);
        data.setIdCardCode("43042219960427000" + i);
        data.setIdCardFront(commonImageUrl);
        data.setIdCardBack(commonImageUrl);
        data.setCreateDate("2024-02-26 10:22:00");
        commonSign(data);
        return data;
    }

    public static SyncDataWorkerDTO workerInfoSync() {
        SyncDataWorkerDTO data = new SyncDataWorkerDTO();
        data.setName("张三");
        data.setPhone("12345678900");
        data.setIdCardCode("430422199604270000");
        data.setIdCardFront(commonImageUrl);
        data.setIdCardBack(commonImageUrl);
        data.setCreateDate("2024-02-26 10:22:00");
        commonSign(data);
        return data;
    }

    /**
     * 任务信息同步
     */
    public static SyncDataTaskDTO taskSync() {
        SyncDataTaskDTO data = new SyncDataTaskDTO();
        data.setTaskId(taskId);
        data.setCompanyId(companyId);
        data.setTaxCreditCode(taxCode);
        data.setTaskName("测试任务_" + System.currentTimeMillis());
        data.setTaskDesc("测试任务描述_" + System.currentTimeMillis());
        data.setCreateDate("2024-02-26 10:22:00");
        data.setTaskCostMin(new BigDecimal("0"));
        data.setTaskCostMax(new BigDecimal("0"));
        data.setTaskTypeName("现代服务");
        data.setBillingCategory("*现代服务*信息服务费");
        commonSign(data);
        return data;
    }

    /**
     * 创客交付物
     */
    public static SyncDataAchievementListDTO achievementSync() {
        SyncDataAchievementListDTO data = new SyncDataAchievementListDTO();
        data.setTaskId(taskId);
        data.setUploadType("ALL");
        data.setAchievementFiles(commonImageUrl);
        data.setAchievementDesc("交付物描述");
        data.setAchievementDate("2024-02-26 10:22:00");
        data.setCreateDate("2024-02-26 10:22:00");

        //如果是某一个人的交付物
//        data.setUploadType("ONE");
//        List<SyncDataAchievementDTO>  syncDataAchievementDTOS = new ArrayList<>();
//        SyncDataAchievementDTO syncDataAchievementDTO = new SyncDataAchievementDTO();
//        syncDataAchievementDTO.setAchievementImg(commonImageUrl);
//        syncDataAchievementDTO.setIdCardCode("430422199604270000");
//        syncDataAchievementDTO.setCreateDate("2024-02-26 10:22:00");
//        syncDataAchievementDTO.setAchievementDesc("交付物描述");
//        syncDataAchievementDTO.setAchievementDate("2024-02-26 10:22:00");
//        syncDataAchievementDTOS.add(syncDataAchievementDTO);

        commonSign(data);
        return data;
    }


    /**
     * 订单信息同步
     */
    public static SyncDataOrderDTO orderSync() {
        SyncDataOrderDTO data = new SyncDataOrderDTO();
        data.setOrderId(orderId);
        data.setTaskId(taskId);
        data.setCompanyId(companyId);
        data.setTaxCreditCode(taxCode);

        BigDecimal realMoney = BigDecimal.ZERO;
        BigDecimal serviceMoney = BigDecimal.ZERO;
        BigDecimal workerMoney = BigDecimal.ZERO;

        //明细不参与加密
        List<SyncDataOrderInfoDTO> orderInfoDTOList = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            SyncDataOrderInfoDTO syncDataOrderInfoDTO = new SyncDataOrderInfoDTO();
            syncDataOrderInfoDTO.setOrderInfoId(orderId + "_" + i);
            syncDataOrderInfoDTO.setName("张三" + i);
            syncDataOrderInfoDTO.setIdCardCode("43042219960427000" + i);
            syncDataOrderInfoDTO.setPhone("1234567890" + i);
            syncDataOrderInfoDTO.setBankCode("62288888888888" + i);
            syncDataOrderInfoDTO.setBankName("招商银行");
            syncDataOrderInfoDTO.setBankAddress("");
            syncDataOrderInfoDTO.setCompositeTax(new BigDecimal("10")); // 10%
            syncDataOrderInfoDTO.setTaskMoney(new BigDecimal(i + 100 + ""));
            syncDataOrderInfoDTO.setRealMoney(new BigDecimal(i + 100 + ""));
            syncDataOrderInfoDTO.setServiceMoney(new BigDecimal("10." + i));
            syncDataOrderInfoDTO.setCompanyPayMoney(syncDataOrderInfoDTO.getRealMoney().add(syncDataOrderInfoDTO.getServiceMoney()));

            realMoney = realMoney.add(syncDataOrderInfoDTO.getRealMoney());
            serviceMoney = serviceMoney.add(syncDataOrderInfoDTO.getServiceMoney());
            workerMoney = workerMoney.add(syncDataOrderInfoDTO.getRealMoney());
            syncDataOrderInfoDTO.setSubPackagePayment(commonImageUrl);
            syncDataOrderInfoDTO.setCreateDate("2024-02-26 10:22:00");

            orderInfoDTOList.add(syncDataOrderInfoDTO);
        }

        data.setRealMoney(realMoney);
        data.setWorkerMoney(workerMoney);
        data.setServiceMoney(serviceMoney);
        data.setTurnkeyProjectPayment(commonImageUrl);
        data.setSubPackagePayment(commonImageUrl);
        // OFFLINE("OFFLINE", "线下支付"),
        //    ALIPAY("ALIPAY", "支付宝支付"),
        //    PABK("PABK","平安银行"),
        //    WZBK("WZBK","温州银行"),
        //    OTHER("OTHER","其它"),
        //    ZSBK("ZSBK","招商银行"),
        //    WECHATPAY("WECHATPAY", "微信支付"),
        data.setPaymentMethod("ZSBK");
        data.setPayDate("2024-02-26 10:22:00");
        data.setCreateDate("2024-02-26 10:22:00");

        commonSign(data);

        data.setOrderInfoDTOList(orderInfoDTOList);
        return data;
    }

    /**
     * 上传回单
     */
    public static SyncDataReceiptDTO receiptSync() {
        SyncDataReceiptDTO data = new SyncDataReceiptDTO();
        data.setOrderId(orderId);

        //明细不参与加密
        List<SyncDataReceiptInfoDTO> syncDataReceiptInfoDTOArrayList = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            SyncDataReceiptInfoDTO syncDataOrderInfoDTO = new SyncDataReceiptInfoDTO();
            syncDataOrderInfoDTO.setUrl(commonImageUrl);
            syncDataOrderInfoDTO.setOrderInfoId(orderId + "_" + i);
            syncDataReceiptInfoDTOArrayList.add(syncDataOrderInfoDTO);
        }

        commonSign(data);

        data.setSyncReceiptInfoDTOList(syncDataReceiptInfoDTOArrayList);
        return data;
    }


    /**
     * 发票同步接口
     */
    public static SyncDataInvoiceDTO invoiceSync() {
        SyncDataInvoiceDTO data = new SyncDataInvoiceDTO();

        //明细不参与加密
        Set<String> ids = new HashSet<>();
        ids.add(orderId);
        ids.add("sync_000000001");
        data.setType(2);
        data.setInvoice(commonImageUrl);
        data.setReceipt(commonImageUrl);

        data.setCreateDate("2024-02-26 10:22:00");
        data.setInvoiceDate("2024-02-26 10:22:00");
        data.setInvoiceCatalogName("*现代服务*信息服务费");
        commonSign(data);

        data.setOrderIdList(ids);

        return data;
    }


    /**
     * 根据服务商ID查询任务类型
     */
    public static void getTaskType(Long taxId) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("taxId", taxId);
        System.out.println("请求参数:" + taxId);
        String post = HttpUtil.post(requestUrl + "task-type", param);
        System.out.println("请求结果:" + post);

    }


    public static <T extends SyncDataDTO> void common(T data) {
        data.setTimestamp(System.currentTimeMillis());
        data.setPlatformId(platformId);
        data.setAgentId(agentId);
    }

    public static <T extends SyncDataDTO> void commonSign(T data) {
//        common(data);
        data.setTimestamp(System.currentTimeMillis());
        data.setPlatformId(platformId);
        data.setAgentId(agentId);
        Map<String, Object> stringObjectMap = JSON.parseObject(JSON.toJSONString(data), new TypeReference<Map<String, Object>>() {
        });
        stringObjectMap.put("key", key);
        data.setSign(ApiSignUtil.getSign(stringObjectMap));
    }

    public static <T extends SyncDataDTO> void post(String url, T data) {
        System.out.println("请求地址:" + url);
        String jsonString = JSON.toJSONString(data);
        System.out.println("请求参数:" + jsonString);
        String post = HttpUtil.post(url, jsonString);
        System.out.println("请求结果:" + post);
    }

    public static String syncId() {
        return "sync_" + System.currentTimeMillis();
    }

}
